package com.android.namecard.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.android.namecard.R;
import com.android.namecard.entity.Namecard;
import com.android.namecard.model.NamecardDBHelper;

public class ChangeGroupActivity extends Activity{
	
	RadioGroup radioGroup;
	String namecardId;
	String namecardType;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.group_select);
		
		Bundle bundle = this.getIntent().getExtras();
		namecardId = bundle.getString("namecardId");
		namecardType = bundle.getString("type");
		
		radioGroup = (RadioGroup)findViewById(R.id.group_radiogroup);
		final RadioButton m_Private = (RadioButton)findViewById(R.id.RadioButton_private);
		final RadioButton m_Commercial = (RadioButton)findViewById(R.id.RadioButton_commercial);
		final RadioButton m_Public = (RadioButton)findViewById(R.id.RadioButton_public);
		Button buttonComfirm = (Button)findViewById(R.id.group_select_comfirm);
		Button buttonCancel = (Button)findViewById(R.id.group_select_cancel);
		
		if(namecardType.equals(NamecardDBHelper.PRIVATE_TYPE)){
			m_Private.setChecked(true);
		}else if(namecardType.equals(NamecardDBHelper.COMMERCIAL_TYPE)){
			m_Commercial.setChecked(true);
		}else if(namecardType.equals(NamecardDBHelper.PUBLIC_TYPE)){
			m_Public.setChecked(true);
		}
		
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
			@Override
			public void onCheckedChanged(RadioGroup arg0, int checkedId) {
				// TODO Auto-generated method stub
				Log.i("you select:................", String.valueOf(checkedId));
			}
		});
		
		buttonComfirm.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO 确定按钮
				int selectId = radioGroup.getCheckedRadioButtonId();
				if(m_Private.getId()==selectId){
					if(!namecardType.equals(NamecardDBHelper.PRIVATE_TYPE)){
						updateNamecardType(NamecardDBHelper.PRIVATE_TYPE);
					}
				}else if(m_Commercial.getId()==selectId){
					if(!namecardType.equals(NamecardDBHelper.COMMERCIAL_TYPE)){
						updateNamecardType(NamecardDBHelper.COMMERCIAL_TYPE);
					}
				}else if(m_Public.getId()==selectId){
					if(!namecardType.equals(NamecardDBHelper.PUBLIC_TYPE)){
						updateNamecardType(NamecardDBHelper.PUBLIC_TYPE);
					}
		        }
				ChangeGroupActivity.this.finish();
			}

			private void updateNamecardType(String type) {
				NamecardDBHelper db = new NamecardDBHelper(getBaseContext());
				db.updateNamecardTypeById(namecardId, type);
				db.close();
			}
		});
		
		buttonCancel.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO 取消按钮
				ChangeGroupActivity.this.finish();
			}
		});
	}
}
